import React, { Component } from "react";
import { View, Text, Image, StyleSheet, TextInput, Keyboard, TouchableWithoutFeedback } from "react-native";
import CustomerHeader from "../../widget/customerHeader";
import  LinearGradient from 'react-native-linear-gradient';
import NetUtil from '../../net/net_util'
import Loading from "../../widget/Loading";
import Toast from "../../widget/Toast";

export default class ModifyItemInfo extends Component{
  constructor(props) {
    super(props);
    callback:null,
    this.state={
      title:'',
      key:'',
      inputTxt:''
    }
  }

  componentWillUnmount() {
    clearInterval(this.timer)
    this.keyboardDidShowListener.remove();
    this.keyboardDidHideListener.remove();
  }
  _keyboardDidShow () {

  }
  _keyboardDidHide () {
    Keyboard.dismiss()
  }

  componentDidMount() {
    let params=this.props.route.params
    this.setState({title:params.title,key:params.key,inputTxt:params.value})
    this.callback=params.refresh;
    this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this._keyboardDidShow);
    this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this._keyboardDidHide);
  }

  /*修改会员信息*/
  updateUserInfo(){
    let params={
      [this.state.key]:this.state.inputTxt
    }
    Loading.show()
    NetUtil.postJson('member/member/modify',params,(res)=>{
      Loading.hide()
      if(res.code==200){
        Toast.add('修改成功')
        setTimeout(()=>{
          this.callback()
          this.props.navigation.pop()
        },1500)
      }
      else {
        Toast.add(res.msg)
      }
    })
  }

  render() {
    return(
      <TouchableWithoutFeedback onPress={()=>{Keyboard.dismiss()}}>
        <View style={{flex:1,backgroundColor:'#fff'}}>
          <CustomerHeader title={'修改昵称'} leftOnPress={()=>{this.props.navigation.pop()}}>
            <LinearGradient colors={['#e79c58','#f8da85']} style={styles.publish}>
              <Text style={styles.publishTxt} onPress={()=>{this.updateUserInfo()}}>提交</Text>
            </LinearGradient>
          </CustomerHeader>
          <View style={{flex:1,backgroundColor:'#F6F6F6'}}>
            <View style={styles.cell}>
              <TextInput style={styles.input} value={this.state.inputTxt} onChangeText={(text)=>{this.setState({inputTxt:text})}} placeholder={'请输入'}></TextInput>
            </View>
          </View>
          <Loading></Loading>
          <Toast></Toast>
        </View>
      </TouchableWithoutFeedback>
    )
  }
}

const styles=StyleSheet.create({
  publish:{
    width:43,
    height:25,
    borderRadius: 22.5,
    marginRight:14
  },
  publishTxt:{
    color:'#fff',
    fontSize: 13,
    lineHeight:25,
    textAlign:'center'
  },
  cell:{
    marginTop:10,
    backgroundColor:'#fff',
    height: 50,
    paddingHorizontal:15
  },
  input:{
    flex:1
  }
})
